package com.ljm.controller;/**
 * @author： li
 * @date： 2022-04-27 11:32
 * @version 1.0
 */

import com.ljm.dao.articleDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author： ljm
 * @date： 2022-04-27 11:32
 * @version 1.0
 */
@WebServlet("/getdetail")
public class detailServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws IOException {
        req.setCharacterEncoding("GBK");
        String id =req.getParameter("id");
        res.setContentType("text/json; charset=utf-8");
        PrintWriter out =res.getWriter();

        try {
           ResultSet rs = articleDao.selectById(id);
            JSONArray ja = new JSONArray();
            JSONObject jo = new JSONObject();
            while (rs.next()){
                jo.put("title",rs.getString("title"));
                jo.put("content",rs.getString("content"));
                jo.put("id",rs.getInt("id"));
                jo.put("date",rs.getString("date"));
                jo.put("img",rs.getString("img"));
                ja.add(jo);
            }
            out = res.getWriter();
            out.println(ja);
        } catch (SQLException e) {
            out.println("ERROR!");
            e.printStackTrace();
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
